package com.rec.smile.contorller;

import java.util.Arrays;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rec.smile.model.Admin;
import com.rec.smile.model.User;

@Controller
public class IndexController {

	@RequestMapping(value="base")
	@ResponseBody
	public String base(@RequestParam("_age")int age){
		return "age = "+age;
	}
	
	@RequestMapping(value="baseObj")
	@ResponseBody
	//public String baseObj(@RequestParam("_age")Integer age){
	public String baseObj(Integer age){
		return "age = "+age;
	}
	
	@RequestMapping(value="array")
	@ResponseBody
	public String baseArray(String[] names){
		return "names = "+Arrays.toString(names);
	}
	
	//TODO req: http://127.0.0.1:8080/user?name=qop&age=100   resp:  User [name=qop, age=100]
	@RequestMapping(value="user")
	@ResponseBody
	public String user(User user){
		return user.toString();
	}
	

	//TODO req: http://127.0.0.1:8080/userAndAdmin?name=qop&age=100   resp:  User [name=qop, age=100],Admin [name=qop, age=100]
	//TODO req: http://127.0.0.1:8080/userAndAdmin?user.name=qop&user.age=100  resp:  User [name=null, age=null],Admin [name=null, age=null]
	//TODO req: http://127.0.0.1:8080/userAndAdmin?user.name=qop&age=99&admin.name=iop  resp: User [name=qop, age=99],Admin [name=iop, age=99]    after databinder
	@RequestMapping(value="userAndAdmin")
	@ResponseBody
	public String userAndAdmin(User user, Admin admin){
		return user+","+admin;
	}
	
	@InitBinder("user")
	public void initDefaultUserBinder(WebDataBinder webDataBinder){
		webDataBinder.setFieldDefaultPrefix("user.");
	}

	@InitBinder("admin")
	public void initDefaultAdminBinder(WebDataBinder webDataBinder){
		webDataBinder.setFieldDefaultPrefix("admin.");
	}
	
	
}
